using GF.Debug;
using GF.ResourcesLoad;
using System.Collections.Generic;
using UnityEngine;

public class AudioMgr : MonoSingleton<AudioMgr>
{
    int change = 0;
    public static int speaker = 1;
    static int sentences = 15;
    public static bool _BGM, _Audio, _Voice;
    static AudioSource BGM, Audio, Voice;
    static AudioClip[] audioClips = new AudioClip[AudioNames.Path.Length];

    private void Awake()
    {
        change = PlayerPrefs.GetInt("Changed");
        if (change != 0)
        {
            speaker = PlayerPrefs.GetInt("speaker");
        }
        _BGM = PlayerPrefs.GetInt("BGM") == 1;
        _Voice = PlayerPrefs.GetInt("Voice") == 1;
        _Audio = PlayerPrefs.GetInt("Audio") == 1;
        BGM = gameObject.AddComponent<AudioSource>();//背景音乐
        Voice = gameObject.AddComponent<AudioSource>();//配音
        Audio = gameObject.AddComponent<AudioSource>();//音效
        BGM.mute = _BGM;
        Voice.mute = _Voice;
        Audio.mute = _Audio;
        initAudio();
    }

    void initAudio()
    {
#if LCS
        FCGAudioCfg kAudioCfg = AppFacade.Instance.ResMgr.Load("FCG/Audio/", "FCGAudioCfg", typeof(FCGAudioCfg)) as FCGAudioCfg;
        if (null != kAudioCfg)
        {
            List<FCGAudioCfg.ItemInfo> kAudioData = kAudioCfg.AudioData;
            foreach (var item in kAudioData)
            {
                FCGAudioCfg.ItemInfo kData = item as FCGAudioCfg.ItemInfo;
                Debugger.Log("加载音效：" + item.ID);
                audioClips[item.ID] = kData.Clip;
            }
        }
#else
        for (int i = 0; i < AudioNames.Path.Length; i++)
        {
            audioClips[i] = CResourceManager.Load<AudioClip>("FCG/Audio/" + AudioNames.Path[i]);
        }
#endif
    }

    public static void SetSpeaker(int i)
    {
        speaker = i;
        PlayerPrefs.SetInt("speaker", speaker);
        PlayerPrefs.SetInt("Changed", 1);
    }

    private void Start()
    {
        BGM.loop = true;
    }

    public static void Play(AudioNames.ID id, bool isBGM)
    {
        Play((int)id, isBGM);
    }

    public static void Play(int id, bool isBGM)
    {
        Debugger.Log("播放声音:" + id);
        if (isBGM)
        {
            BGM.clip = audioClips[id];
            BGM.Play();
        }
        else
        {
            Audio.clip = audioClips[id];
            Audio.Play();
        }
    }

    public static void PlayVoice(AudioNames.ID id)
    {
        PlayVoice((int)id + speaker * sentences);
    }

    public static void PlayVoice(int id)
    {
        if (WebSocketManager.instance.PlayerPM().fishMatchCursor < 2)
        {
            if (id == 22 || id == 37)  //引导不需要欢迎语音
                return;
        }
        Debugger.Log("播放音效:" + id);
        Voice.clip = audioClips[id];
        Voice.Play();
    }

    public static void MuteBGM(bool isOn)
    {
        _BGM = isOn;
        BGM.mute = isOn;
        if (isOn)
        {
            
            PlayerPrefs.SetInt("BGM", 1);
        }
        else
        {
            PlayerPrefs.SetInt("BGM", 0);
        }
    }

    public static void MuteSound(bool isOn)
    {
        _Audio = isOn;
        Audio.mute = isOn;
        Voice.mute = isOn;
        if (isOn)
        {
            PlayerPrefs.SetInt("Voice", 1);
            PlayerPrefs.SetInt("Audio", 1);
        }
        else
        {
            PlayerPrefs.SetInt("Voice", 0);
            PlayerPrefs.SetInt("Audio", 0);
        }
    }

    public static void StopBGM()
    {
        BGM.Stop();
    }

    public static void StopAudio()
    {
        Audio.Stop();
    }

    public static void PlayRandomBGM()
    {
        Play(14 + Random.Range(0, 3), true);
    }
}
